package com.xunyin.modules.goods.rest;

import com.xunyin.api.ApiResult;
import com.xunyin.common.interceptor.AuthCheck;
import com.xunyin.modules.log.AppLog;
import com.xunyin.modules.goods.domain.TGoodsOrder;
import com.xunyin.modules.goods.domain.TTeaRoomContainerGoodsLink;
import com.xunyin.modules.goods.service.TGoodsOrderService;
import com.xunyin.modules.goods.service.TTeaRoomContainerGoodsLinkService;
import com.xunyin.modules.goods.service.param.GoodsOrderParam;
import com.xunyin.modules.goods.service.param.TeaRoomContainerGoodsLinkParam;
import com.xunyin.utils.constants.ErrorEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.SortedMap;

/**
 * <p>
 * 茶室柜台商品订单控制器
 * </p>
 *
 * @author admin
 * @since 2022-10-28
 */
@Slf4j
@RestController
@Api(value = "商品订单", tags = "共享空间:商品订单")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GoodsOrderController {

    private final TTeaRoomContainerGoodsLinkService tTeaRoomContainerGoodsLinkService;

    private final TGoodsOrderService tGoodsOrderService;


    @AppLog(value = "根据userId获取商品订单列表", type = 1)
    //@AuthCheck
    @PostMapping("/order-goods/list")
    @ApiOperation(value = "根据userId获取商品订单列表", notes = "根据userId获取商品订单列表")
    public ApiResult<List<TGoodsOrder>> listGoodsOrderByCreateUserId(@Validated @RequestBody GoodsOrderParam param) {
        List<TGoodsOrder> list = tGoodsOrderService.listGoodsOrderByCreateUserId(param);
        return ApiResult.ok(list);
    }

    /**
     * 管理员查询茶室消费商品数量接口
     */
    @AppLog(value = "管理员查询茶室消费商品数量接口", type = 1)
    //@AuthCheck
    @PostMapping("/getGoodsOrderRecordByTeaHouseId")
    @ApiOperation(value = "管理员查询茶室消费商品数量接口", notes = "管理员查询茶室消费商品数量接口")
    public ApiResult<List<TTeaRoomContainerGoodsLink>> getGoodsOrderRecordByTeaHouseId(@Validated @RequestBody GoodsOrderParam param) {
        List<TTeaRoomContainerGoodsLink> list = tTeaRoomContainerGoodsLinkService.getGoodsOrderRecordByTeaHouseId(param.getTeaHouseId());
        return ApiResult.ok(list);
    }

    /**
     * 货登记设备打开指令
     */
    @AppLog(value = "货登记设备打开指令", type = 1)
    @AuthCheck
    @PostMapping("/supplement")
    @ApiOperation(value = "货登记设备打开指令", notes = "货登记设备打开指令")
    public ApiResult<String> supplement(@Validated @RequestBody GoodsOrderParam param) {

        String resultStr = tGoodsOrderService.supplement(param.getGoodOrderId());
        if ("success".equals(resultStr)) {
            return ApiResult.ok("补货成功");
        } else {
            return ApiResult.fail(ErrorEnum.E_100022.getErrorMsg());
        }
    }

    /**
     * 用户柜台商品下单支付
     *
     * @return ApiResult<Map < String, Object>>
     */
    @AppLog(value = "用户柜台商品下单支付", type = 1)
    //@AuthCheck
    @PostMapping("/goodsOrderPay")
    @ApiOperation(value = "用户柜台商品下单支付", notes = "用户柜台商品下单支付")
    public ApiResult<SortedMap<String, String>> orderPay(@Validated @RequestBody GoodsOrderParam param) {
        log.info("goodsOrderPay -> {}", param.toString());
        return ApiResult.ok(tGoodsOrderService.goodsOrderPay(param));
    }

    /**
     * 柜台商品下单后库存自减
     *
     * @return ApiResult<String>
     */
    @AppLog(value = "柜台商品下单后库存自减", type = 1)
    //@AuthCheck
    @PostMapping("/reduceStock")
    @ApiOperation(value = "柜台商品下单后库存自减", notes = "柜台商品下单后库存自减")
    public ApiResult<String> reduceStock(@Validated @RequestBody TeaRoomContainerGoodsLinkParam param) {
        tTeaRoomContainerGoodsLinkService.reduceStock(param.getId());
        return ApiResult.ok("成功");
    }


}
